﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.MemberContact;
using ECommerce.Model.MemberContact;
using Jzy.Core.Common;
using Jzy.Core.Utility;
using Jzy.Core.Common.Web;
using ECommerce.Common;

namespace ECommerce.Web.Site
{
    public partial class MemberContact : SitePage
    {
        private ECMemberContactBO Contact
        {
            get
            {
                return ECMemberContactBO.Instance<ECMemberContactBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.CheckLogin();
            if (!IsPostBack)
            {
                List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(MemorialType));
                drpType.DataSource = list;
                drpType.DataTextField = "Name";
                drpType.DataValueField = "ID";
                drpType.DataBind();

                BindView();
            }
            WebUtil.SetTimeBox(this.MemorialDay, false);
        }

        private void BindView()
        {
            List<ECMemberContactInfo> list = Contact.GetList(SiteSession.MemberID);
            rpt.DataSource = list;
            rpt.DataBind();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            ECMemberContactInfo info = new ECMemberContactInfo();
            if (hidID.Value != "")
            {
                info.ID = int.Parse(hidID.Value);
            }

            info.MemberID = SiteSession.MemberID;
            info.Name = Name.Text;
            info.Telephone = Telephone.Text;
            info.MemorialDay = DateTime.Parse(MemorialDay.Text);
            info.Address = Address.Text;
            info.TypeID = int.Parse(drpType.SelectedValue);

            this.Contact.Save(info);
            BindView();

            ClearInfo();
        }

        private void DispInfo(int id)
        {
            ECMemberContactInfo info = this.Contact.GetInfo(id);
            hidID.Value = info.ID.ToString();
            Name.Text = info.Name;
            Telephone.Text = info.Telephone;
            Address.Text = info.Address;
            MemorialDay.Text = info.MemorialDay.ToString("yyyy-MM-dd");
            drpType.SelectedValue = info.TypeID.ToString();
        }

        private void ClearInfo()
        {
            hidID.Value = "";
            Name.Text = "";
            Telephone.Text = "";
            Address.Text = "";
            MemorialDay.Text = "";
            drpType.SelectedIndex = 0;

        }

        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "Delete")
            {
                this.Contact.Delete(id);
                ClearInfo();
                BindView();
            }
            else if (e.CommandName == "Edit")
            {
                DispInfo(id);
            }
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = (LinkButton)e.Item.FindControl("btnDelete");
                btnDelete.OnClientClick = "return confirm('确认删除')";
            }
        }

       
    }
}